//
//  InputUserInfoViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/6.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "InputUserInfoViewController.h"

@interface InputUserInfoViewController () <UITextFieldDelegate>

/// 模型视图
@property (nonatomic, readonly, strong) InputUserInfoViewModel* viewModel;

@property (nonatomic, readwrite, strong) NSMutableDictionary* viewDic;

// UI

//IBOutlet
@property (weak, nonatomic) IBOutlet UIView* inputTFView;
@property (weak, nonatomic) IBOutlet UIView* inputTextView;
@property (weak, nonatomic) IBOutlet UIView* inputCompanyView;
@property (weak, nonatomic) IBOutlet UIButton* confirmBtn;

@end

@implementation InputUserInfoViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    _viewDic = [NSMutableDictionary dictionary];
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
    @weakify(self);
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
    
    [[[viewDidLayoutSVSignal take:1] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    [[self.confirmBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        
        [MBProgressHUD mh_showProgressHUD:@""];
        
        [self _inputAllValue];
    }];
    
    [[self.viewModel.reqCmd.executionSignals.switchToLatest deliverOnMainThread] subscribeNext:^(id x)
    {
        @strongify(self);
        
        [MBProgressHUD mh_hideHUD];
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [MBProgressHUD mh_showTips:@"修改成功"];
    }];
    [[self.viewModel.reqCmd.errors deliverOnMainThread] subscribeNext:^(NSError* error)
     {
         [MBProgressHUD mh_hideHUD];
         
         /// 处理验证错误的error
         if ([error.domain isEqualToString:SUCommandErrorDomain]) {
             [MBProgressHUD mh_showTips:error.userInfo[SUCommandErrorUserInfoKey]];
             return ;
         }
         [MBProgressHUD mh_showErrorTips:error];
     }];
}

#pragma mark - 更新初始化 操作
- (void)_setupAllView
{
    @weakify(self);
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    [self _rebuildViewDic];
    
    //0:TF 1:TextView 2:Company2 3:Company3
    if (self.viewModel.type == 0)
    {
        [self _showHideView:self.inputTFView tag:self.inputTFView.tag isShow:YES];
        [self _showHideView:self.inputTextView tag:self.inputTextView.tag isShow:NO];
        [self _showHideView:self.inputCompanyView tag:self.inputCompanyView.tag isShow:NO];
        
        UITextField* inputTF = [self.inputTFView viewWithTag:100];
        inputTF.delegate = self;
        inputTF.text = self.viewModel.strTF;
        RAC(self.viewModel, strTF) = [inputTF rac_textSignal];
    }
    else if (self.viewModel.type == 1)
    {
        [self _showHideView:self.inputTFView tag:self.inputTFView.tag isShow:NO];
        [self _showHideView:self.inputTextView tag:self.inputTextView.tag isShow:YES];
        [self _showHideView:self.inputCompanyView tag:self.inputCompanyView.tag isShow:NO];
        
        UIView* contentView = [self.inputTextView viewWithTag:10];
        
        YYTextView* tView = [[YYTextView alloc] initWithFrame:(CGRect) {0, 0, contentView.frame.size}];
        tView.font = [UIFont systemFontOfSize:16.0f];
        tView.placeholderFont = [UIFont systemFontOfSize:16.0f];
        tView.placeholderText = @"请输入";
        tView.placeholderTextColor = UIColorHex(#f2f2f2);
        tView.tag = 100;
        [contentView addSubview:tView];
        tView.editable = self.viewModel.editable;
        tView.text = self.viewModel.strText;
    }
    else if (self.viewModel.type == 2)
    {
        [self _showHideView:self.inputTFView tag:self.inputTFView.tag isShow:NO];
        [self _showHideView:self.inputTextView tag:self.inputTextView.tag isShow:NO];
        [self _showHideView:self.inputCompanyView tag:self.inputCompanyView.tag isShow:YES];
        
        UIView* conpanyNameView = [self.inputCompanyView viewWithTag:10];
        {
            UITextField* tf = [conpanyNameView viewWithTag:100];
            tf.text = self.viewModel.strCompanyName;
            tf.delegate = self;
            UILabel* titleLabel = [conpanyNameView viewWithTag:101];
            titleLabel.text = [self.viewModel.title containsString:@"企业"] ? @"企业名" : @"机构名";
        }
        
        UIView* nameView = [self.inputCompanyView viewWithTag:11];
        {
            UITextField* tf = [nameView viewWithTag:110];
            tf.text = self.viewModel.strCompanyContacter;
            tf.delegate = self;
        }
        
        UIView* mobileView = [self.inputCompanyView viewWithTag:12];
        {
            UITextField* tf = [mobileView viewWithTag:120];
            tf.text = self.viewModel.strCompanyMobile;
            tf.delegate = self;
        }
        
        UIView* addressView = [self.inputCompanyView viewWithTag:13];
        {
            UIView* contentView = [addressView viewWithTag:130];
            
            YYTextView* tView = [[YYTextView alloc] initWithFrame:(CGRect) {0, 0, contentView.frame.size}];
            tView.font = [UIFont systemFontOfSize:16.0f];
            tView.placeholderFont = [UIFont systemFontOfSize:16.0f];
            tView.placeholderText = @"请输入联系地址";
            tView.placeholderTextColor = UIColorHex(#f2f2f2);
            tView.tag = 1000;
            [contentView addSubview:tView];
            tView.editable = self.viewModel.editable;
            tView.text = self.viewModel.strCompanyAddr;
            
//            //隐藏
//            for (NSLayoutConstraint* curConstraint in addressView.constraints)
//            {
//                if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
//                {
//                    curConstraint.constant = 0;
//
//                    break;
//                }
//            }
        }
    }
//    else if (self.viewModel.type == 3)
//    {
//        [self _showHideView:self.inputTFView tag:self.inputTFView.tag isShow:NO];
//        [self _showHideView:self.inputTextView tag:self.inputTextView.tag isShow:NO];
//        [self _showHideView:self.inputCompanyView tag:self.inputCompanyView.tag+1 isShow:YES];
//
//        UIView* conpanyNameView = [self.inputCompanyView viewWithTag:10];
//        {
//            UITextField* tf = [conpanyNameView viewWithTag:100];
//            UILabel* titleLabel = [conpanyNameView viewWithTag:101];
//            titleLabel.text = @"机构名称";
//        }
//
//        UIView* nameView = [self.inputCompanyView viewWithTag:11];
//        {
//            UITextField* tf = [nameView viewWithTag:100];
//        }
//
//        UIView* mobileView = [self.inputCompanyView viewWithTag:12];
//        {
//            UITextField* tf = [mobileView viewWithTag:100];
//        }
//
//        UIView* addressView = [self.inputCompanyView viewWithTag:13];
//        {
//            UIView* contentView = [addressView viewWithTag:100];
//
//            YYTextView* tView = [[YYTextView alloc] initWithFrame:(CGRect) {0, 0, contentView.frame.size}];
//            tView.font = [UIFont systemFontOfSize:16.0f];
//            tView.placeholderFont = [UIFont systemFontOfSize:16.0f];
//            tView.placeholderText = @"请输入";
//            tView.placeholderTextColor = UIColorHex(#f2f2f2);
//            tView.tag = 1000;
//            [contentView addSubview:tView];
//        }
//    }
    
    self.confirmBtn.hidden = !self.viewModel.editable;
}

#pragma mark - Delegate 一些无法用RAC实现
// UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return self.viewModel.editable;
}

// UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

#pragma mark-代理方法
// 设立实现代理，注意要返回UIModalPresentationNone
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}

#pragma mark - 功能
- (void)_inputAllValue
{
    //0:TF 1:TextView 2:Company2 3:Company3
    if (self.viewModel.type == 0)
    {
        UITextField* inputTF = [self.inputTFView viewWithTag:100];
        self.viewModel.strTF = inputTF.text;
    }
    else if (self.viewModel.type == 1)
    {
        UIView* contentView = [self.inputTextView viewWithTag:10];
        
        YYTextView* tView = [contentView viewWithTag:100];
        self.viewModel.strText = tView.text;
    }
    else if (self.viewModel.type == 2)
    {
        UIView* nameView = [self.inputCompanyView viewWithTag:10];
        {
            UITextField* tf = [nameView viewWithTag:100];
            self.viewModel.strCompanyName = tf.text;
        }
        
        UIView* contacterView = [self.inputCompanyView viewWithTag:11];
        {
            UITextField* tf = [contacterView viewWithTag:110];
            self.viewModel.strCompanyContacter = tf.text;
        }
        
        UIView* mobileView = [self.inputCompanyView viewWithTag:12];
        {
            UITextField* tf = [mobileView viewWithTag:120];
            self.viewModel.strCompanyMobile = tf.text;
        }
        
        UIView* addressView = [self.inputCompanyView viewWithTag:13];
        {
            UIView* contentView = [addressView viewWithTag:130];
            YYTextView* tView = [contentView viewWithTag:1000];
            self.viewModel.strCompanyAddr = tView.text;
        }
    }
//    else if (self.viewModel.type == 3)
//    {
//        UIView* introView = [self.inputCompanyView viewWithTag:10];
//        {
//            UIView* contentView = [introView viewWithTag:100];
//            YYTextView* tView = [contentView viewWithTag:1000];
//            self.viewModel.strCompanyIntro = tView.text;
//        }
//
//        UIView* contactView = [self.inputCompanyView viewWithTag:11];
//        {
//            UITextField* tf = [contactView viewWithTag:100];
//            self.viewModel.strCompanyContact = tf.text;
//        }
//
//        UIView* serContentView = [self.inputCompanyView viewWithTag:12];
//        {
//            UIView* contentView = [serContentView viewWithTag:100];
//            YYTextView* tView = [contentView viewWithTag:1000];
//            self.viewModel.strCompanySerContent = tView.text;
//        }
//    }
    
    if (self.viewModel.reqCmd)
        [self.viewModel.reqCmd execute:nil];
}

- (void)_rebuildViewDic
{
    [_viewDic setObject:@(self.inputTFView.frame.size.height) forKey:@(self.inputTFView.tag)];
    [_viewDic setObject:@(self.inputTextView.frame.size.height) forKey:@(self.inputTextView.tag)];
    [_viewDic setObject:@(self.inputCompanyView.frame.size.height) forKey:@(self.inputCompanyView.tag)];
}

- (void)_showHideView:(UIView*)view tag:(NSInteger)tag isShow:(BOOL)isShow
{
    CGFloat height = [_viewDic[@(tag)] floatValue];
    
    for (NSLayoutConstraint* curConstraint in view.constraints)
    {
        if (curConstraint.firstAttribute == NSLayoutAttributeHeight)
        {
            curConstraint.constant = isShow?height:0;
            
            break;
        }
    }
}

#pragma mark - 跳转

@end
